package cn.sc.summer.rabbitmq.config;

import cn.sc.summer.constant.rabbit.RabbitConstant;
import org.springframework.amqp.core.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 类名：网关日志配置类
 *
 * @author a-xin
 * @date 2024/2/5 10:48
 */
@Configuration
@ConditionalOnClass(value = RabbitConfig.class)
public class GatewayLogConfig extends RabbitConfig {

    @Bean(RabbitConstant.GATEWAY_LOG_EXCHANGE)
    public DirectExchange gatewayLogExchange() {
        return ExchangeBuilder
                .directExchange(RabbitConstant.GATEWAY_LOG_EXCHANGE)
                .durable(true)
                .build();
    }

    @Bean(RabbitConstant.GATEWAY_LOG_BINDING)
    public Binding gatewayLogBinding() {
        return BindingBuilder.bind(gatewayLogQueue()).to(gatewayLogExchange()).with(RabbitConstant.GATEWAY_LOG_ROUTING_KEY);
    }

    @Bean(RabbitConstant.GATEWAY_LOG_QUEUE)
    public Queue gatewayLogQueue() {
        Queue queue = QueueBuilder.durable(RabbitConstant.GATEWAY_LOG_QUEUE)
                .ttl(RabbitConstant.GATEWAY_LOG_TIME_OUT)
                .deadLetterExchange(RabbitConstant.DEAD_EXCHANGE)
                .deadLetterRoutingKey(RabbitConstant.DEAD_EXCHANGE_ROUTING_KEY)
                .build();
        rabbitAdmin().declareQueue(queue);
        return queue;
    }

}
